package com.youhome.service.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.youhome.common.exception.CustomException;
import com.youhome.common.response.Result;
import com.youhome.common.response.ResultCode;
import com.youhome.common.response.ResultPage;
import com.youhome.service.mapper.BShareGroupHouseMapper;
import com.youhome.service.model.BCounselor;
import com.youhome.service.model.BShareGroupHouse;
import com.youhome.service.model.BUser;
import com.youhome.service.model.ListHub;
import com.youhome.service.service.BCounselorService;
import com.youhome.service.service.BShareGroupHouseService;
import com.youhome.service.service.BUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

/**
 * Created by chen365 on 2018/05/14.
 */
@Service
public class BShareGroupHouseServiceImpl implements BShareGroupHouseService {
    @Autowired
    private BShareGroupHouseMapper bShareGroupHouseMapper;
    @Autowired
    private BUserService bUserService;
    @Autowired
    private BCounselorService counselorService;

    @Override
    public PageInfo<ListHub> selectHouseByShareId(Integer pageIndex, Integer pageSize, String shareId) {
        if (pageIndex == null || pageIndex < 1)
            pageIndex = 1;

        if (pageSize == null || pageSize < 1)
            pageSize = 10;

        PageHelper.startPage(pageIndex, pageSize);
        List<ListHub> list = bShareGroupHouseMapper.selectHouseByShareId(shareId);
        return new PageInfo<>(list);
    }

    @Override
    public List<ListHub> selectHouseByShareId(String shareId) {
        return bShareGroupHouseMapper.selectHouseByShareId(shareId);
    }

    @Override
    public int deleteByPrimaryKey(Long id) {
        return bShareGroupHouseMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(BShareGroupHouse bShareGroupHouse) {
        bShareGroupHouse.setCtime(new Date());
        return bShareGroupHouseMapper.insert(bShareGroupHouse);
    }

    @Override
    public PageInfo<BShareGroupHouse> selectByBean(Integer pageNumber, Integer pageSize, BShareGroupHouse record) {
        if (StringUtils.isEmpty(pageNumber) || pageNumber < 1) {
            pageNumber = 1;
        }
        if (StringUtils.isEmpty(pageSize) || pageSize < 10) {
            pageSize = 10;
        }
        PageHelper.startPage(pageNumber, pageSize);
        List<BShareGroupHouse> bShareGroupHouses = bShareGroupHouseMapper.selectByBean(record);
        return new PageInfo<>(bShareGroupHouses);
    }

    @Override
    public PageInfo<BShareGroupHouse> selectByBean(String phone, String shareId, Integer pageNumber, Integer pageSize) throws CustomException {

        BUser bUser = bUserService.selectByPhone(phone);
        BCounselor bCounselor = null;
        if (bUser != null) {
            bCounselor = counselorService.selectByUserId(bUser.getUserId());
        }
        if (bCounselor == null) {
            throw new CustomException(ResultCode.LAND_EXPIRATION);
        }
        if (StringUtils.isEmpty(shareId)) {
            throw new CustomException(ResultCode.ERROR_PARAMETER);
        }
        BShareGroupHouse record = new BShareGroupHouse();
        record.setShareId(shareId);
        if (StringUtils.isEmpty(pageNumber) || pageNumber < 1) {
            pageNumber = 1;
        }
        if (StringUtils.isEmpty(pageSize) || pageSize < 10) {
            pageSize = 10;
        }
        PageHelper.startPage(pageNumber, pageSize);
        List<BShareGroupHouse> bShareGroupHouses = bShareGroupHouseMapper.selectByBean(record);
        return new PageInfo<>(bShareGroupHouses);
    }

    @Override
    public void insert(String phone, String shareId, String houseId) throws CustomException {
        BUser bUser = bUserService.selectByPhone(phone);
        BCounselor bCounselor = null;
        if (bUser != null) {
            bCounselor = counselorService.selectByUserId(bUser.getUserId());
        }
        if (bCounselor == null) {
            throw new CustomException(ResultCode.LAND_EXPIRATION);
        }
        BShareGroupHouse bShareGroupHouse = new BShareGroupHouse();
        if (StringUtils.isEmpty(shareId)) {
            throw new CustomException(ResultCode.ERROR_PARAMETER);
        }
        bShareGroupHouse.setShareId(shareId);
        if (StringUtils.isEmpty(houseId)) {
            throw new CustomException(ResultCode.ERROR_PARAMETER);
        }
        bShareGroupHouse.setHouseId(houseId);
        bShareGroupHouse.setCtime(new Date());
        if (bShareGroupHouseMapper.insert(bShareGroupHouse) == 0) {
            throw new CustomException(ResultCode.FAIL);
        }
    }

    @Override
    public void delete(String phone, String shareId, Long id) throws CustomException {
        if (StringUtils.isEmpty(id)) {
            throw new CustomException(ResultCode.ERROR_PARAMETER);
        }
        BUser bUser = bUserService.selectByPhone(phone);
        BCounselor bCounselor = null;
        if (bUser != null) {
            bCounselor = counselorService.selectByUserId(bUser.getUserId());
        }
        if (bCounselor == null) {
            throw new CustomException(ResultCode.LAND_EXPIRATION);
        }
        if (bShareGroupHouseMapper.deleteByPrimaryKey(id) == 0) {
            throw new CustomException(ResultCode.FAIL);
        }
    }
}
